﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotnet.Utils.Utility.Zip
{
    public class CompressUtil
    {
        /// <summary>  
        /// 用.net自带的Gzip对二进制数组进行压缩
        /// </summary>  
        /// <param name="data">二进制数组</param>  
        /// <returns>压缩后二进制数组</returns>  
        public static byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Compress, true);
            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            ms.Position = 0;
            byte[] compressed_data = new byte[ms.Length];
            ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
            return compressed_data;
        }
        /// <summary>
        /// 用.net自带的Gzip对二进制数组进行解压
        /// </summary>
        /// <param name="data">二进制数组</param>
        /// <returns>解压后二进制数组</returns>
        public static byte[] Decompress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Decompress, true);
            zipStream.Write(data, 0, data.Length);
            zipStream.Close();
            ms.Position = 0;
            byte[] decompressed_data = new byte[ms.Length];
            ms.Read(decompressed_data, 0, int.Parse(ms.Length.ToString()));
            return decompressed_data;
        }
    }
}
